//
//  LGECGDetailMedicineCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/9/10.
//  ECG医学指标

#import "LGECGDetailMedicineCell.h"

@interface LGECGDetailMedicineCell()
/**   */
@property (nonatomic , strong)UIView *backView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *valueLabel;
/**   */
@property (nonatomic , strong)UILabel *referenceTitle;
/**   */
@property (nonatomic , strong)UILabel *referenceLabel;
/**   */
@property (nonatomic , strong)UILabel *statusLabel;
/**   */
@property (nonatomic , strong)UIImageView *tipView;
/**   */
@property (nonatomic , assign)NSInteger minValue;
/**   */
@property (nonatomic , assign)NSInteger maxValue;
/**   */
@property (nonatomic , copy)NSString *unit;

@end

@implementation LGECGDetailMedicineCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.type = LGPositionTypeMiddle;
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    self.unit = LGNSString(dataDic[@"unit"]);
    self.minValue = [dataDic[@"min"] integerValue];
    self.maxValue = [dataDic[@"max"] integerValue];
    NSString *content = @"";
    if (self.minValue > 0) {
        content = [NSString stringWithFormat:@"%ld~%ld %@",self.minValue,self.maxValue,self.unit];
    }else{
        content = [NSString stringWithFormat:@"<%ld %@",self.maxValue,self.unit];
    }
    self.referenceLabel.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(15),NSForegroundColorAttributeName:[UIColor colorWithString:@"#909399"]} range:NSMakeRange(content.length-self.unit.length, self.unit.length)];
}

-(void)setValue:(NSInteger)value{
    _value = value;
    NSString *content = [NSString stringWithFormat:@"%ld %@",value,self.unit];
    self.valueLabel.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(15),NSForegroundColorAttributeName:[UIColor colorWithString:@"#909399"]} range:NSMakeRange(content.length-self.unit.length, self.unit.length)];
    CGFloat tipW = 0;
    if (self.minValue > 0) {
        if (value<self.minValue) {
            //偏低
            self.statusLabel.text = @"偏低";
            self.statusLabel.textColor = [UIColor colorWithString:@"#5E8EF5"];
            self.tipView.image = [UIImage imageNamed:@"监测-偏低"];
            
        }else if (value>self.maxValue){
            //偏高
            self.statusLabel.text = @"偏高";
            self.statusLabel.textColor = [UIColor colorWithString:@"#C14D4D"];
            self.tipView.image = [UIImage imageNamed:@"监测-偏高"];
            
        }else{
            //正常
            tipW = viewPix(16);
            self.statusLabel.text = @"正常";
            self.statusLabel.textColor = [UIColor colorWithString:@"#282828"];
            self.tipView.image = [UIImage imageNamed:@""];
        }
    }else{
        if (value<self.maxValue) {
            //正常
            tipW = viewPix(16);
            self.statusLabel.text = @"正常";
            self.statusLabel.textColor = [UIColor colorWithString:@"#282828"];
            self.tipView.image = [UIImage imageNamed:@""];
            
        }else{
            //偏高
            self.statusLabel.text = @"偏高";
            self.statusLabel.textColor = [UIColor colorWithString:@"#C14D4D"];
            self.tipView.image = [UIImage imageNamed:@"监测-偏高"];
        }
    }
    [self.tipView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(tipW));
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.contentView addSubview:self.backView];
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.valueLabel];
    [self.backView addSubview:self.referenceTitle];
    [self.backView addSubview:self.referenceLabel];
    [self.backView addSubview:self.statusLabel];
    [self.backView addSubview:self.tipView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(6));
        make.bottom.equalTo(self.contentView).offset(-viewPix(6));
        make.left.equalTo(self.contentView).offset(viewPix(24));
        make.right.equalTo(self.contentView).offset(-viewPix(24));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(viewPix(10));
        make.left.equalTo(self.backView).offset(viewPix(16));
    }];
    
    [self.referenceTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(12));
        make.bottom.equalTo(self.backView).offset(-viewPix(10));
        make.left.equalTo(self.titleLabel);
    }];
    [self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView.mas_centerX).offset(-viewPix(20));
        make.centerY.equalTo(self.titleLabel);
    }];
    [self.referenceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.valueLabel);
        make.centerY.equalTo(self.referenceTitle);
    }];
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.tipView.mas_left);
        make.centerY.equalTo(self.tipView);
    }];
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backView).offset(-viewPix(10));
        make.centerY.equalTo(self.backView);
        make.width.height.equalTo(@(viewPix(16)));
    }];
}

-(UIView *)backView{
    if(!_backView){
        _backView = [[UIView alloc]init];
        _backView.backgroundColor = [UIColor colorWithString:@"#F1F7FF"];
        _backView.cornerRidus = 5;
    }
    return _backView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)valueLabel{
    if(!_valueLabel){
        _valueLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _valueLabel;
}

-(UILabel *)referenceTitle{
    if(!_referenceTitle){
        _referenceTitle = [UILabel lableWithText:@"参考值" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _referenceTitle;
}

-(UILabel *)referenceLabel{
    if(!_referenceLabel){
        _referenceLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _referenceLabel;
}

-(UILabel *)statusLabel{
    if(!_statusLabel){
        _statusLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _statusLabel;
}

-(UIImageView *)tipView{
    if(!_tipView){
        _tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"监测-偏高"]];
    }
    return _tipView;
}

@end
